import request from '@/utils/request'
import * as cacheApi from '@/utils/cache'

function formatRequestUri(name) {
  return 'hr/TreeDic/' + name
}

export function getTrees(dicId, pid) {
  const key = 'DicTree_' + dicId + '_' + pid
  const data = cacheApi.getCache(key)
  if (data != null) {
    return new Promise((resolve) => {
      resolve(data)
    })
  } else {
    return new Promise((resolve, reject) => {
      request({
        url: formatRequestUri('GetDicTree'),
        method: 'get',
        params: {
          dicId,
          pid
        }
      }).then((res) => {
        cacheApi.setCache(key, res)
        resolve(res)
      }).catch(reject)
    })
  }
}
export function getTree(query) {
  return request({
    url: formatRequestUri('GetTree'),
    method: 'post',
    data: query
  })
}

export function DeleteItem(id) {
  return request({
    url: formatRequestUri('Delete'),
    method: 'get',
    params: {
      id
    }
  })
}

export function Add(data) {
  return request({
    url: formatRequestUri('Add'),
    method: 'post',
    data
  })
}

export function Set(id, data) {
  return request({
    url: formatRequestUri('Set'),
    method: 'post',
    data: {
      Id: id,
      Datum: data
    }
  })
}
export function Stop(id) {
  return request({
    url: formatRequestUri('Stop'),
    method: 'get',
    params: {
      id
    }
  })
}
export function Enable(id) {
  return request({
    url: formatRequestUri('Enable'),
    method: 'get',
    params: {
      id
    }
  })
}
export function Get(id) {
  return request({
    url: formatRequestUri('Get'),
    method: 'get',
    params: {
      id
    }
  })
}
export function Move(id, toId) {
  return request({
    url: formatRequestUri('Move'),
    method: 'post',
    data: {
      FromId: id,
      ToId: toId
    }
  })
}

export function GetNames(dicId, vals) {
  return request({
    url: formatRequestUri('GetNames'),
    method: 'post',
    data: {
      Id: dicId,
      Value: vals
    }
  })
}
